package org.example;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

/**
 * @author zhangjg
 * @desc
 * @date 2023/05/18 19:37
 **/
@Slf4j
public class Excel2 {

    public static void main(String[] args) {
        List<OBean> map_wg_1 = new ArrayList<>();
        Map<String, List<Object>> map_jf_5 = new HashMap<String, List<Object>>();
        Map<String, List<Object>> map_jf_6 = new HashMap<String, List<Object>>();

        int cnt = 0;
        ExcelReader reader_jf = ExcelUtil.getReader(FileUtil.file("D:/tmp/ndb/数据比对(基础表)/基础对比/在网网元数量-20230505.xlsx"), 3);
        List<List<Object>> list_jf = reader_jf.read(1);
        for (List<Object> l : list_jf) {
            //id,省份,地市,运营商,网络类型,设备型号,网管系列
            //1,2,3,4,9,11,24
            //1,2,3,8,10,23
//            log.info("{},{},{},{},{},{},{}",l.get(1),l.get(2),l.get(3),l.get(4),l.get(9),l.get(11),l.get(24));
            //省份,专业,运营商,设备型号,设备数量  -> 补充地市
            //1,8,3,10,11
            if (!StrUtil.isEmpty(l.get(2).toString())) {
                map_jf_5.put(l.get(1).toString() + l.get(8) + l.get(3) + l.get(10) + l.get(11), l);
                map_jf_6.put(l.get(1).toString() + l.get(2) + l.get(8) + l.get(3) + l.get(10) + l.get(11), l);
                cnt++;
            }
        }
        log.info("cnt={},size5={},size6={}", cnt, map_jf_5.keySet().size(), map_jf_6.keySet().size());

        ExcelWriter ew = ExcelUtil.getWriter("D:/tmp/ndb/数据比对(基础表)/基础对比/2.xlsx");
        List<List<Object>> tlist = CollUtil.newArrayList();

        int c1 = 0;
        int c2 = 0;
        ExcelReader reader_wg = ExcelUtil.getReader(FileUtil.file("D:/tmp/ndb/数据比对(基础表)/基础对比/网管工程数据明细.xlsx"), 2);
        List<List<Object>> list_wg = reader_wg.read(1);
        List<Object> headList = reader_wg.read(0).get(0);
        tlist.add(headList);
        for (List<Object> l : list_wg) {
//          id,省份,地市,运营商,网络类型,设备型号,网管系列
//          1,2,3,11,5,6,12
//          0,1,2,10,4,5,11
//            log.info("{},{},{},{},{},{},{}",l.get(1),l.get(2),l.get(3),l.get(11),l.get(5),l.get(6),l.get(12));
            //省份,专业,运营商,设备型号,设备数量  -> 补充地市
            //1,4,10,5,7
            if (StrUtil.isEmpty(l.get(2).toString())) {
                List<Object> list = new ArrayList<>();
                for (int i = 0; i < l.size(); i++) {
                    if (i == 2) {
                        List<Object> list1 = map_jf_5.get(l.get(1).toString() + l.get(4) + l.get(10) + l.get(5) + l.get(7));
                        if(list1 == null){
                            list.add(l.get(i));
                            c2++;
                        }else {
                            list.add(list1.get(2));
                        }
                    } else {
                        list.add(l.get(i));
                    }
                }
                tlist.add(list);
                c1++;
            } else {
                tlist.add(l);
            }
        }

        ew.write(tlist);
        ew.flush();
        ew.close();
        log.info("---end c1={},c2={}",c1,c2);

    }

}
